package com.example.day02.mefrag;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.day02.Custom.DLRoundImageView;
import com.example.day02.R;
import com.example.day02.utils.AssetsUtils;

import static android.content.Context.MODE_PRIVATE;


/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends Fragment implements View.OnClickListener {
    private static final String TAG = "MeFragment";
    TextView aboutTv;
    TextView introduce;
    TextView update;
    TextView problem;
    TextView starTv;
    String name;
    String show_logo_name;
    DLRoundImageView imageView;
    SharedPreferences sp;
    SharedPreferences.Editor editor;
    String logoname;
    public MeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        Bundle bundle = getArguments();
        name = bundle.getString("name");
        show_logo_name = bundle.getString("show_logo_name");
        Log.d(TAG, "show_logo_name: " + show_logo_name);
        sp = getContext().getSharedPreferences("logo_pref", MODE_PRIVATE);

        logoname = sp.getString("logoname", show_logo_name);
        Log.d(TAG, "logoname: " + logoname);
        initView(view);


        if (TextUtils.isEmpty(logoname)) {
            editor = sp.edit();
            editor.putString("logoname", show_logo_name);
            editor.commit();
            imageView.setImageResource(R.mipmap.nologo);
        } else {
            String filename = "xzcontentlogo/" + logoname + ".png";
            Bitmap bm = AssetsUtils.getBitmapByAssets(getContext(), filename);
            imageView.setImageBitmap(bm);
        }

        return view;
    }

    private void initView(View view) {
        aboutTv = view.findViewById(R.id.mefrag_tv_about);
        aboutTv.setOnClickListener(this);
        introduce = view.findViewById(R.id.mefrag_tv_introduce);
        introduce.setOnClickListener(this);
        update = view.findViewById(R.id.mefrag_tv_update);
        update.setOnClickListener(this);
        problem = view.findViewById(R.id.mefrag_tv_problem);
        problem.setOnClickListener(this);
        imageView = view.findViewById(R.id.mefrag_iv);
        imageView.setOnClickListener(this);
        starTv = view.findViewById(R.id.chose_star_name);
        starTv.setText(name);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.mefrag_tv_about:
                intent.setClass(getContext(), AboutActivity.class);
                startActivity(intent);
                break;
            case R.id.mefrag_tv_introduce:
                intent.setClass(getContext(), IntroduceActivity.class);
                startActivity(intent);
                break;
            case R.id.mefrag_tv_update:
                intent.setClass(getContext(), UpdateActivity.class);
                startActivity(intent);
                break;
            case R.id.mefrag_tv_problem:
                intent.setClass(getContext(), ProblemActivity.class);
                startActivity(intent);
                break;
            case R.id.mefrag_iv:
                intent.setClass(getContext(), LogoActivity.class);
                startActivity(intent);
                //存储价值对数据
                sp = getContext().getSharedPreferences("logo_pref", MODE_PRIVATE);
                editor = sp.edit();
                editor.putString("logoname", show_logo_name);
                editor.commit();
                String filename = "xzcontentlogo/" + show_logo_name + ".png";
                Bitmap bm1 = AssetsUtils.getBitmapByAssets(getContext(), filename);
                imageView.setImageBitmap(bm1);
                break;
        }

    }
}
